#include "PreviewWidget.h"
#include <QIODevice>
#include <QImageReader>
#include <QTextStream>
#include <QtDebug>
#include <QMouseEvent>
#include <QScrollArea>
#include <QTime>
#include <QDesktopWidget>


PreviewWidget::PreviewWidget(QWidget *parent, QIODevice *image, QString ImageName, QString mime)
	: QWidget(parent), canceled_(false), done_(false), todelete_(false)
{
	ui.setupUi(this);
	this->image_.Image = image;
	image->reset();
	QImageReader imgReader(image);
	this->image_.mimeType = mime;
	QSize sizeRes = imgReader.size();
	this->image_.size = image->size();
	qreal niceSize;
	QString imageSizeStr;
	if ( (niceSize = qreal(size()) / qreal(1024) / qreal(1024) ) > 1 )
	{ imageSizeStr = QString::number(niceSize, 'f', 2)+" MBytes"; }
	else if ( (niceSize = size() / qreal(1024)) > 1 )
	{ imageSizeStr = QString::number(niceSize, 'f', 2)+" KBytes"; }
	else
	{ imageSizeStr = QString::number(size())+" Bytes"; }

	ui.label_3->setText(
				QString::number(sizeRes.width())
				+"x"
				+QString::number(sizeRes.height())
				+ ", " +imageSizeStr
				+ ", " +mime	);


	ui.ImgNameLabel->setText(ImageName);
	this->image_.ImageName = ImageName;
	connect(ui.RemoveButton, SIGNAL(clicked()), this, SLOT(removeMe_()));
	connect(ui.RemoveButton, SIGNAL(clicked()), this, SLOT(deleteLater()));
#ifndef NOPREVIEW
// MEMO:	imgReader.format() == "png"
	pc = new PreviewCreator(this->image_.Image, previewSize());
	connect(pc, SIGNAL(previewDone(QImage)), this, SLOT(setPreview(QImage)));
	pc->start();
#endif
}



void PreviewWidget::removeMe_()
{ emit removeMe(this); }

void PreviewWidget::uploadCanceled()
{
	this->ui.progressBar->setFormat(tr("Canceled"));
	canceled_ = true;
	done_ = false;
}

void PreviewWidget::uploadFinished()
{
	if ( ! isCanceled() ) {
		this->ui.progressBar->setFormat(tr("Done"));
		this->ui.progressBar->setMaximum(100);
		this->ui.progressBar->setValue(100);
		done_ = true;
	}
}

void PreviewWidget::uploadStarted()
{
	this->ui.progressBar->setFormat(tr("%p% Uploaded"));
	this->ui.progressBar->setValue(0);
	canceled_ = false;
	done_ = false;
}

void PreviewWidget::uploadError(QString error)
{
	this->ui.progressBar->setFormat(tr("Error"));
	this->ui.progressBar->setToolTip(error);
	canceled_ = true;
	done_ = false;
}

void PreviewWidget::setProgress(int val, int max)
{
	qreal cur = qreal(ui.progressBar->value())/qreal(ui.progressBar->maximum());
	qreal neW = qreal(val)/qreal(max);
	this->lastDelta_ = qint64((neW-cur)*qreal(this->size()));
	this->ui.progressBar->setMaximum(max);
	this->ui.progressBar->setValue(val);
}

qreal PreviewWidget::currentProgress() const
{
	return qreal(this->ui.progressBar->value())/qreal(this->ui.progressBar->maximum());
}












#ifndef NOIMAGEWINDOWPREVIEW
void PreviewWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
	qDebug() << "mouse event:" << event->button();
	if ( event->button() != 1 and event->button() != 2 )
		return;
	QWidget *imageView = new QWidget();
	QVBoxLayout *layoutView = new QVBoxLayout(imageView);
	imageView->setLayout(layoutView);
	QScrollArea *scroller = new QScrollArea(imageView);
	QLabel *imageViewLabel;
	QPushButton *quit_ = new QPushButton(tr("Close"), imageView);
	connect(quit_, SIGNAL(clicked()), imageView, SLOT(deleteLater()));
	layoutView->addWidget(quit_);
	if ( this->Image()->isOpen() ) {
		this->Image()->reset();
	} else {
		this->Image()->open(QIODevice::ReadOnly);
	}
	QPixmap seeMe; seeMe.loadFromData(this->Image()->readAll());
	///// TODO: calculate more nice pixmap sizes
	if ( event->button() == 1 )
//			or ( (QApplication::desktop()->availableGeometry().size().width() - 100) > seeMe.width()
//				and (QApplication::desktop()->availableGeometry().size().height() - 100) > seeMe.height() )
//		)
	{
		if ( (QApplication::desktop()->availableGeometry().size().width() - 100) < seeMe.width()
			or (QApplication::desktop()->availableGeometry().size().height() - 100) < seeMe.height() )
		{
			qDebug() << "button 1 too large";
			QSize imgSize = QApplication::desktop()->availableGeometry().size();
			imgSize.scale(imgSize.width() - 100, imgSize.height() - 100, Qt::KeepAspectRatio);
			imageViewLabel = new QLabel(imageView);
			imageViewLabel->setPixmap(seeMe.scaled(imgSize, Qt::KeepAspectRatio));
			imageView->layout()->addWidget(imageViewLabel);
			imageView->resize(QSize(imageViewLabel->size() + quit_->size()));
			scroller->deleteLater();
		} else {
			qDebug() << "button 1 NOT too large";
			imageViewLabel = new QLabel(imageView);
			imageViewLabel->setPixmap(seeMe);
			//scroller->setWidget(imageViewLabel);
			scroller->deleteLater();
			imageView->layout()->addWidget(imageViewLabel);
			//imageView->resize(QSize(imageViewLabel->size() + QSize(100,100)));
			qDebug() << imageViewLabel->size() << imageView->size();
		}
	} else { // button 2
			qDebug() << "button 2";
			imageViewLabel = new QLabel(scroller);
			imageViewLabel->setPixmap(seeMe);
			scroller->setWidget(imageViewLabel);
			imageView->layout()->addWidget(scroller);
			imageView->resize(imageViewLabel->size());
			qDebug() << imageViewLabel->size() << imageView->size();
	}
	imageView->show();
}
#endif //NOIMAGEWINDOWPREVIEW

#ifndef NOPREVIEW
void PreviewCreator::run() {
	QImage preview(size, QImage::Format_ARGB32);
	image->reset();
	if ( preview.load(image, "") ) {
		emit previewDone(preview.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
	}
	quit();
}
#endif
